package org.gordonframework.site.views {
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	
	public class GViewPort extends GView {
		
		private var _view:DisplayObject;
		private var _scaleMode:String;
		
		public function GViewPort(view:DisplayObject = null, width:Number = 320, height:Number = 240, scaleMode:String = "none") {
			this.view = view;
			this.scrollRect = new Rectangle(0, 0, width, height);
			this.scaleMode = scaleMode;
		}
		
		public function set view(value:DisplayObject):void {
			if(_view) {
				removeChild(_view);
			}
			_view = value;
			if(_view) {
				addChild(_view);
			}
		}
		
		public function get view():DisplayObject {
			return _view;
		}
		
		public function set scaleMode(value:String):void {
			_scaleMode = value;
		}
		
		override public function set width(value:Number):void{
			setSize(value, height);
		}
		
		override public function get width():Number {
			return scrollRect.width;
		}
		
		override public function set height(value:Number):void {
			setSize(width, value);
		}
		
		override public function get height():Number {
			return scrollRect.height;
		}
		
		public function get scaleMode():String {
			return _scaleMode;
		}
		
		public function set scrollX(value:Number):void {
			setScroll(value, scrollY);
		}
		
		public function get scrollX():Number {
			return scrollRect.x;
		}
		
		public function set scrollY(value:Number):void {	
			setScroll(scrollX, value);
		}
		
		public function get scrollY():Number {
			return scrollRect.y;
		}
		
		public function get maxScrollX():Number {
			if(view && view.width > scrollRect.width) {
				return view.width - scrollRect.width;
			}
			return 0;
		}
		
		public function get maxScrollY():Number {
			if(view && view.height > scrollRect.height) {
				return view.height - scrollRect.height;
			}
			return 0;
		}
		
		public function setSize(width:Number, height:Number):void {
			var rect:Rectangle = scrollRect;
			rect.width = width;
			rect.height = height,
			scrollRect = rect;
		}
		
		public function setScroll(x:Number, y:Number):void {
			var rect:Rectangle = scrollRect;
			rect.x = Math.max(0, Math.min(maxScrollX, x));
			rect.y = Math.max(0, Math.min(maxScrollY, y));
			scrollRect = rect;	
		}
		
	}
}